<?php
namespace Openapi\Controller;
use Think\Controller;
class OrdersController extends CommonController {
	
	/**
	* 退款接口
	*@param $mid 商户ID
	*@param $site_id 网站ID
	* @param $oid  平台订单号
	* @param $refund_amount 退款金额，与订单金额货币一样
	* @param $hash_info 验证信息  sha256  mid.site_id.$oid.$refund_amount.site_key
	* @return json 返回退款是否成功的JSON格式信息
	*/
	public function refund($mid="",$site_id="",$oid="",$refund_amount="",$hash_info=""){
		$result['status']=0;		
		if(!parent::prepare($mid,$site_id,$oid)){
		$result['info']="transaction not found";
		$this->ajaxReturn($result);
		}

		$reg_amount="/^([1-9]\d*)(\.)(\d{2})$/";
		if((float)$refund_amount<1 || !preg_match($reg_amount,$refund_amount)){
			$result['info']='invalid refund amount';
			$this->ajaxReturn($result);	
		} 
			//dump($this->order_info);
		if($this->order_info['order_status']!=2 &&
		$this->order_info['order_status']!=4 && 
		$this->order_info['order_status']!=8){
			$result['info']='invalid order status';
			$this->ajaxReturn($result);	
		}
	
		//查看加密信息是否一致
		//echo $this->site_key;
		$hash_info1=hash("sha256",$mid.$site_id.$oid.$refund_amount.$this->site_key);
		//echo $mid.$site_id.$oid.$refund_amount.$this->site_key;exit;
		if($hash_info != $hash_info1){
			$result['info']='invalid request';
			$this->ajaxReturn($result);	
		}
	
	
		//1.修改订单状态 2.记录订单历史 3.返回成功信息
		$op=new \Common\Common\OrderProcess($oid);
		$rs=$op->toPendingRefund($refund_amount);
		//dump($rs);exit;
	
		if($rs){
			$result['status']=1;
			$result['info']="refund request successfully sent";
			$this->ajaxReturn($result);
		}else{
			$result['info']="error,please try again";
			$this->ajaxReturn($result);				
		}
	}
	
	/**
	*取消退款的接口
	*@param $mid 商户ID
	*@param $site_id 网站ID
	* @param $oid  平台订单号
	*@param string hash_info 验证信息 sha256
	*@return  json 返回取消是否成功的信息
	*/
	public function cancelRefund($mid="",$site_id="",$oid="",$hash_info=""){
		$result['status']=0;		
		if(!parent::prepare($mid,$site_id,$oid)){
		$result['info']="transaction not found";
		$this->ajaxReturn($result);
		}

		if($this->order_info['order_status']!=7){
			$result['info']='invalid order status';
			$this->ajaxReturn($result);				
		}
		//查看加密信息是否一致
		$hash_info1=hash("sha256",$mid.$site_id.$oid.$this->site_key);
		if($hash_info1 != $hash_info){
			$result['info']='invalid request';
			$this->ajaxReturn($result);	
		}
		//1.修改订单状态 2.记录订单历史 3.返回成功信息
		$op=new \Common\Common\OrderProcess($oid);
		$rs=$op->toApprove();
		//dump($rs);exit;
	
		if($rs){
			$result['status']=1;
			$result['info']="cancel refund request successfully sent";
			$this->ajaxReturn($result);
		}else{
			$result['info']="error,please try again";
			$this->ajaxReturn($result);				
		}		
				
	}
	
	/**
	*获取一笔交易的相关情况
	*@param $mid 商户ID
	*@param $site_id 网站ID
	* @param $oid  平台订单号
	* @param $hash_info 验证信息  
	* @return json 返回该笔订单的JSON格式信息	
	*/
	public function getInfo($mid="",$site_id="",$oid="",$hash_info=""){
		$result['status']=0;		
		if(!parent::prepare($mid,$site_id,$oid)){
		$result['info']="transaction not found";
		$this->ajaxReturn($result);
		}
		//查看加密信息是否一致
		$hash_info1=hash("sha256",$mid.$site_id.$oid.$this->site_key);
		if($hash_info != $hash_info){
			$result['info']='invalid  request';
			$this->ajaxReturn($result);	
		}
		//如果验证没有问题了，就返回JSON
		$arr['order_id']=(int)$list["order_id"];
		$arr['merchant_oid']=$list["mh_id"];
		$arr['order_time']=$list["order_time"];
		$arr['order_status']=(int)$list['order_status'];
		$arr['order_amount']=$list['order_amount'];
		$arr['order_currency']=$list["order_currency"];
		$arr["card_type"]=$list["card_type"];
		$arr["order_site"]=$list["site_domain"];
		$arr["billing_descriptor"]=$list["order_bill_desc"];
		$arr["billing_email"]=$list["bill_email"];
		$arr["billing_country"]=$list["bill_country"];
		$arr["billing_state"]=$list["bill_state"];
		$arr["billing_city"]=$list["bill_city"];
		$arr["billing_street"]=$list["bill_street"];
		$arr["billing_phone"]=$list["bill_phone"];
		$arr["process_mode"]=$list["process_mode"]?"sales":"authorize";
		
		$result["status"]=1;
		$result["info"]="success";
		$result["data"]=$arr;
		$this->ajaxReturn($result);
		
	}

	/**
	* 取消一笔预授权交易
	*@param $mid 商户ID
	*@param $site_id 网站ID
	* @param $oid  平台订单号
	* @param $hash_info 验证信息  SHA256
	* @return json 返回该笔订单是否取消成功的JSON格式信息	
	*/
	public function void($mid="",$site_id="",$oid="",$hash_info=""){
		$result['status']=0;		
		if(!parent::prepare($mid,$site_id,$oid)){
		$result['info']="transaction not found";
		$this->ajaxReturn($result);
		}
		if($this->order_info['order_status']!=3 &&
		$this->order_info['order_status']!=1
		){
			$result['info']='invalid order status';
			$this->ajaxReturn($result);				
		}
		//查看加密信息是否一致
		$hash_info1=hash("sha256",$mid.$site_id.$oid.$this->site_key);
		if($hash_info != $hash_info1){
			$result['info']='invalid request';
			$this->ajaxReturn($result);	
		}
		//1.修改订单状态 2.记录订单历史 3.返回成功信息
		$op=new \Common\Common\OrderProcess($oid);
		$rs=$op->toVoid();
	
		if($rs){
			$result['status']=1;
			$result['info']="void request successfully sent";
			$this->ajaxReturn($result);
		}else{
			$result['info']="error,please try again";
			$this->ajaxReturn($result);				
		}			
	}
	

}